<template>
  <section class="todoapp">
    <!-- 除了驼峰, 还可以使用-转换链接 -->
    <TodoHeader  @enter="enterFn" :list="list"></TodoHeader>
    <TodoMain :list="showList" @del="delFn"></TodoMain>
    <TodoFooter :getSel="getSel" @harry="harry" @delb="delbFn" :len="this.list.length"></TodoFooter>
  </section>
</template>

<script>
// 1.0 样式引入
import "./styles/base.css"
import "./styles/index.css"

import TodoHeader from "./components/TodoHeader";
import TodoMain from "./components/TodoMain";
import TodoFooter from "./components/TodoFooter";



export default {
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  data() {
    return {
      list: JSON.parse(localStorage.getItem('todolist')) || [],
      getSel:'all'
    };
  },
  // 事件监听list的变化，并存在本地
  watch: {
    list: {
      handler() {
        localStorage.setItem('todolist',JSON.stringify(this.list))
      }
    }
  },
  methods: {
    // 新增列表
    enterFn(val) {
      this.list.length === 0 ? this.list.push({ id: 100, name: val, isDone: false }) : this.list.push({id: this.list.at(-1).id+1, name: val, isDone: false})
    },
    // 删除功能
    delFn(id) {
      this.list = this.list.filter(item => item.id !== id)
    },
    // 显示选中对应的内容
    harry(type) {
      this.getSel = type
    },
    // 删除已完成
    delbFn() {
      this.list = this.list.filter(item => item.isDone === false)
    }
  },
  computed: {
    // 显示已完成，未完成和全部的转换
    showList() {
      if(this.getSel === 'yes') {
        return this.list.filter(item => item.isDone === true)
      }else if(this.getSel === 'no') {
        return this.list.filter(item => item.isDone === false)
      } else {
        return this.list
      }
    }
  }
};
</script>
